package com.compass.doctor.ui.setting.viewmodel

import androidx.lifecycle.MutableLiveData
import com.compass.doctor.R
import com.compass.framework.utils.LogUtil
import com.compass.framework.toast.TipsToast
import com.compass.framework.utils.getStringFromResource
import com.compass.network.viewmodel.BaseViewModel
import com.netease.yunxin.kit.common.ui.viewmodel.FetchResult
import com.netease.yunxin.kit.common.ui.viewmodel.LoadStatus
import com.netease.yunxin.kit.corekit.im.provider.FetchCallback
import com.netease.yunxin.kit.corekit.im.repo.SettingRepo


/**
 * Description： 消息提醒设置viewModel
 * Date:2023/11/23 16:10
 */
class SettingNotifyViewModel : BaseViewModel() {


    val notifyDetailLiveData = MutableLiveData<FetchResult<Boolean>>()

    val toggleNotificationLiveDataLiveData = MutableLiveData<FetchResult<Boolean>>()


    fun getToggleNotification(): Boolean {
        return SettingRepo.isPushNotify()
    }

    fun setToggleNotification(value: Boolean) {
        SettingRepo.setMessageNotification(value)
        SettingRepo.setPushNotify(value, object : FetchCallback<Void> {
            override fun onSuccess(param: Void?) {
                TipsToast.showTips(getStringFromResource(R.string.setting_success))
                val fetchResult = FetchResult<Boolean>(LoadStatus.Success)
                fetchResult.data = value
                toggleNotificationLiveDataLiveData.postValue(fetchResult)
            }

            override fun onFailed(code: Int) {
                TipsToast.showTips(getStringFromResource(R.string.setting_fail))

                LogUtil.e("loadStatus==$code")
                val fetchResult = FetchResult<Boolean>(LoadStatus.Error)
                fetchResult.data = value
                toggleNotificationLiveDataLiveData.postValue(fetchResult)
            }

            override fun onException(exception: Throwable?) {
                LogUtil.e("loadStatus==${exception.toString()}")
                TipsToast.showTips(getStringFromResource(R.string.setting_fail))
                val fetchResult = FetchResult<Boolean>(LoadStatus.Error)
                fetchResult.data = value
                toggleNotificationLiveDataLiveData.postValue(fetchResult)
            }
        })
    }

    fun getRingToggle(): Boolean {
        return SettingRepo.getRingMode()
    }

    fun setRingToggle(ring: Boolean) {
        SettingRepo.setRingMode(ring)
    }

    fun getVibrateToggle(): Boolean {
        return SettingRepo.getVibrateMode()
    }

    fun setVibrateToggle(mode: Boolean) {
        SettingRepo.setVibrateMode(mode)
    }

    fun getPushShowNoDetail(): Boolean {
        return !SettingRepo.getPushShowDetail()
    }


    fun setPushShowNoDetail(mode: Boolean) {
        SettingRepo.setPushShowDetail(
            mode,
            object : FetchCallback<Void> {
                override fun onSuccess(param: Void?) {
                    TipsToast.showTips(getStringFromResource(R.string.setting_success))
                    val fetchResult = FetchResult<Boolean>(LoadStatus.Success)
                    fetchResult.data = mode
                    notifyDetailLiveData.postValue(fetchResult)
                }

                override fun onFailed(code: Int) {
                    TipsToast.showTips(getStringFromResource(R.string.setting_fail))
                    val fetchResult = FetchResult<Boolean>(LoadStatus.Error)
                    fetchResult.data = mode
                    notifyDetailLiveData.postValue(fetchResult)
                }

                override fun onException(exception: Throwable?) {
                    TipsToast.showTips(getStringFromResource(R.string.setting_fail))
                    val fetchResult = FetchResult<Boolean>(LoadStatus.Error)
                    fetchResult.data = mode
                    notifyDetailLiveData.postValue(fetchResult)
                }
            })
    }
}